//
//  SHStatusOriginalView.swift
//  SHWeibo01
//
//  Created by 卫平玉 on 16/7/23.
//  Copyright © 2016年 卫平玉. All rights reserved.
//

import UIKit
import SnapKit
class SHStatusOriginalView: UIView {

    var orginalViewBottomConstraint: Constraint?
    var statusViewModel: SHStatusViewModel?{
        didSet{
            headImageView.sh_setImage(statusViewModel?.status?.user?.profile_image_url, placeholderImgName: "avatar_default")
            nameLabel.text = statusViewModel?.status?.user?.name
            memberImageView.image = statusViewModel?.memberImage
            avatarImageView.image = statusViewModel?.avatarImage
            timeLabel.text = statusViewModel?.sinaTimeStr
            sourceLabel.attributedText = statusViewModel?.sourceAttr
            
            contentLabel.text = statusViewModel?.status?.text
            dealContentText(statusViewModel?.status?.text)

            
            orginalViewBottomConstraint?.uninstall()
            
            if statusViewModel?.status?.pic_urls?.count > 0 {
                
                pictureView.pictureInfo = statusViewModel?.status?.pic_urls
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(SHStatusCellMargin).constraint
                })
                
                pictureView.hidden = false
            }else{
                
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(SHStatusCellMargin).constraint
                })
                
                pictureView.hidden = true
            }
            
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func dealContentText(text: String?){
//        print(text)
        
        guard let t = text else{
            
            return
        }
//        正则表达式
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
        
    (t as NSString).enumerateStringsMatchedByRegex(
        regex) { (count, string, range, _) -> Void in
            print(string.memory)
            let emoticon = SHEmoticonTools.sharedTools.searchEmoticons((string.memory as? String))
              print(emoticon?.png)
        }

    }
    
    func setupUI(){
      backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        
        //添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(SHStatusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(SHStatusCellMargin)
        }
        
        memberImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(SHStatusCellMargin)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(SHStatusCellMargin)
            
            make.bottom.equalTo(timeLabel)
        }
        
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing).offset(-2)
            make.centerY.equalTo(headImageView.snp_bottom).offset(-2)
        }
        
        //07
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(headImageView)
           make.top.equalTo(headImageView.snp_bottom).offset(SHStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(SHStatusCellMargin)
              make.leading.equalTo((SCREENW - (cellWH*3 + cellMargin*2))/2)
            
//              make.trailing.equalTo(20)
        }
        
        self.snp_makeConstraints { (make) -> Void in
            self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(SHStatusCellMargin).constraint
        }
        
    }
    
    
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    
    private lazy var nameLabel: UILabel = UILabel(textColor: UIColor.blackColor(), textFont: 15)
    private lazy var memberImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    
    private lazy var timeLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    
    private lazy var sourceLabel: UILabel = UILabel(textColor: ThemeColor, textFont: 10)
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), textFont: 15,maxWidth: SCREENW - 2 * SHStatusCellMargin)
    
    private lazy var pictureView: SHStatusPictureView = SHStatusPictureView()

}

extension SHStatusOriginalView{
    
    // 处理微博认证用户图片/// 认证类型 -1：没有认证，1，认证用户，2,3,5: 企业认证，220: 达人
    func getAvatarImage(verified: Int?) -> UIImage?{
        
        guard let ver = verified else{
            
            return nil
        }
        
        switch ver {
            
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
            
        case 220:
            return UIImage(named: "avatar_grassroot")
            
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
    
    func getMemberImage(mbrank: Int?) -> UIImage?{
        
        guard let mb = mbrank else{
            
            return nil
        }
        
        if mb > 0 && mb < 7 {
            
            return UIImage(named: "common_icon_membership_level\(mb)")
        }else{
            return UIImage(named: "common_icon_membership")
        }
    }
}